package com.lsmo.android.carfans.activity.car;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.car.Brand;
import com.lsmo.android.carfans.process.car.BrandListProcess;
import com.lsmo.android.carfans.view.car.SelectCarView;
import com.lsmo.android.common.base.BaseActivity;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

public class BrandListActivity extends BaseActivity implements ViewProcessCallBack {
	
	public static final String TAG = SelectCarView.class.getSimpleName();
	
	public ListView mListView;
	public Context mContext ;
	private ViewGroup mProgressBarLayout;
	private LayoutInflater mInflater ;
	private BrandListAdapter yqAdapter,jkAdapter;
	private ArrayList<Brand> BrandList,BrandList1,BrandList2;
	private BrandListProcess bListProcess;
	private String[] params = null;
	public static int ids,positions;
	public static String names;
	
	public static void launch(Context c,int position,int id,String name){
		Intent intent = new Intent(c, BrandListActivity.class);
		ids = id;
		positions = position;
		names = name;
		c.startActivity(intent);
	}
	
 	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInflater = this.getLayoutInflater();
		this.bListProcess = new BrandListProcess();
		this.mContext = BrandListActivity.this;
		
		initView();//加载view
		
		updateView();//加载页面数据
	}
 	
 	/**
 	 * @author SYX
 	 * 加载主体的view
 	 */
 	public void initView(){
 		this.setContentView(R.layout.select_view_only_list);
 		mListView = (ListView)this.findViewById(R.id.listView);
 		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
 		mListView.setOnItemClickListener(mBrandListItemClickListener);//注册listItem监听
 		TextView titleView = (TextView)this.findViewById(R.id.title_bar_layout_tv_id);
 		titleView.setGravity(Gravity.CENTER);
 		titleView.setText(names);
 	}
 	
 	@Override
	public void updateView() {
		
		// TODO Auto-generated method stub
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.mListView.setVisibility(View.GONE);
		   
			new BrandListTask(BrandListTask.GET_BRANDLIST,
					this.mContext,
					this,
					this.bListProcess 
		 ).execute(new String[]{});
	}

	@Override
	public void processAfterTaskFinish(int code, Object result) {
	 
		if(code == BrandListTask.GET_BRANDLIST){
			if(result!=null){
				setBrandList();
			}
		}
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.mListView.setVisibility(View.VISIBLE);
	}
 	
 	
 	private class BrandListTask extends BaseTask{
 		
 		public static final int GET_BRANDLIST =5;
		private BrandListProcess bListProcess ;
		
		public BrandListTask(int code , Context context,ViewProcessCallBack view,BaseProcess process ) {
			super(code,context,view );
			this.bListProcess = (BrandListProcess) process;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
			    
				case GET_BRANDLIST:
					
					//加载数据
					result = (ArrayList<Brand>) bListProcess.getYqList(params, ids);
					BrandList = (ArrayList<Brand>) result;
					
					break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			switch(this.mCode){
				 
				case GET_BRANDLIST:
					
				 		mView.processAfterTaskFinish(this.mCode ,result );
				 	
				 	break;
		   }
		}
 		
 	};
 	
 	
 	/**
 	 * @author SYX
 	 * 加载listview中的数据
 	 */
 	public void setBrandList(){
 		yqAdapter = new BrandListAdapter(this);//一汽
		jkAdapter = new BrandListAdapter(this);//进口
		//ArrayList<Brand> BrandList1 = downloadData();
		ArrayList<Brand> BrandList2 = downloadData2();
		
		yqAdapter.setList(BrandList);
		jkAdapter.setList(BrandList2);
 	    // separate adapters on one list
		SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);
		separatedAdapter.addSection("一汽奥迪", yqAdapter);
		separatedAdapter.addSection("进口奥迪", jkAdapter);
		
		mListView.setAdapter(separatedAdapter);
 	}

 	public ArrayList<Brand> downloadData(){
 		BrandList1 = (ArrayList<Brand>) bListProcess.getYqList(params, ids);
 		return BrandList1;
 	}
	
 	public ArrayList<Brand>  downloadData2(){
 		BrandList2 = (ArrayList<Brand>) bListProcess.getJKList(params, ids);
 		return BrandList2;
 	}
 	
 	/**
 	 * listview监听事件
 	 */
 	private OnItemClickListener mBrandListItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int index,
				long time) {
			int position = index;
			BrandList2 = downloadData2();
			if(BrandList.size() > 0 && BrandList.size() <=5 && position <5){
				int id = ((Brand)BrandList.get(index)).getId();
			    String name = ((Brand)BrandList.get(index)).getName();
				BrandTypeActivity.launch(mContext,position,id,name);
			}
			else if(BrandList2.size()>0 && BrandList2.size() <=5 && position <5){
				int id = ((Brand)BrandList2.get(index)).getId();
			    String name = ((Brand)BrandList2.get(index)).getName();
				BrandTypeActivity.launch(mContext,position,id,name);
			}
			
		}
	};
		
}
